package com.zx.controller;

import com.zx.common.ResponseCode;
import com.zx.entity.Comment;
import com.zx.repository.FoodRepository;
import com.zx.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@RequestMapping("/comments")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @PostMapping("/saveComments")
    public ModelAndView saveComments(Comment comment,
                                     HttpSession session,
                                     Map<String,Object> map){
        if(session.getAttribute("loginUser").equals(null)){
            map.put("msg", ResponseCode.NEED_LOGIN.getDesc());
            map.put("url","/foodshare/user/ordinary");
            return new ModelAndView("common/error",map);
        };
        commentService.saveComment(comment);
        map.put("msg",ResponseCode.COMMENT_SUCCESS.getDesc());
        map.put("url","/foodshare/main/foodDetail/"+comment.getFoodId());
        return new ModelAndView("common/success",map);
    }
}
